package com.weishenai.customerassistant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weishenai.customerassistant.entity.Substation;
import com.weishenai.customerassistant.mapper.SubstationMapper;
import com.weishenai.customerassistant.service.SubstationService;
import com.weishenai.customerassistant.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class SubstationServiceImpl extends ServiceImpl<SubstationMapper, Substation> implements SubstationService {

    @Autowired
    private SubstationMapper substationMapper;

    @Override
    public Integer insert(Substation substation) {
        substation.setCreatedTime(DateUtils.getTime());
        return substationMapper.insert(substation);
    }

    @Override
    public Integer delSubstation(Integer substationId) {
        QueryWrapper<Substation> q = new QueryWrapper<>();
        q.eq("id", substationId);

        return substationMapper.delete(q);
    }

    @Override
    public Integer updateSubstation(Substation substation) {
        substation.setCreatedTime(DateUtils.getTime());
        return substationMapper.updateById(substation);
    }

    @Override
    public IPage<Substation> selectAll(Integer pageNumber, Integer pageSize) {
        return substationMapper.selectPage(new Page<>(pageNumber, pageSize),
                Wrappers.lambdaQuery(Substation.class).orderByDesc(Substation::getCreatedTime));
    }

    @Override
    public List<Substation> selectAll() {
        return substationMapper.selectList(Wrappers.lambdaQuery(Substation.class));
    }

    @Override
    public IPage<Substation> selectByKeyWord(Integer pageNumber, Integer pageSize, String keyWord) {
        return substationMapper.selectPage(new Page<>(pageNumber, pageSize),
                Wrappers.lambdaQuery(Substation.class)
                        .like(Objects.nonNull(keyWord), Substation::getSubstationName, keyWord));
    }
}
